(function(mui, document) {
	mui.init({
		preloadPages: [{
			url: 'impdateAdd.html',
			id: 'impdateAdd'
		}],
		gestureConfig : {
			longtap:true
		}
	});
	var tapId = null;
	mui.plusReady(function() {
        initList();
		on('#impDateList li', 'longtap', function(){
			tapId = $(this).data('id');
			mui('.mui-popover').popover('toggle');
		});
		
		on('.doneBtn', 'tap', function() {
			var $li = $(this).parent().parent();
			tapId = $li.data('id');
			console.log(tapId);
			var params = {
				'entity.id': tapId,
			}
			app.ajax('impDateAction!do_delete', params, function(data) {
				initList();
				mui('.mui-popover').popover('toggle');
			});
		});
		
	});
	function on(obj, event, func){
		$(document).off(event, obj).on(event, obj, func);
	};
	function showList(ul) {
		if(ul.find('li').size() > 0 && ul.is(':hidden')) ul.show();
	}
	function initList() {
		app.ajax('impDateAction!to_list', null, function(data) {
			var $ul = $('#impDateList').empty();
			mui.each(data, function(i, item) {
				$ul.append(genLi(item));
			});
			showList($ul);
		});
	}
	function genLi(data) {
		var id = data.id;
		var realDate = data.realDate;
		var name = data.name;
		var dateOf = data.dateOf;
		var type = data.type;
		var note = data.note;
		var subDate = data.subDate;
		var li =
			'<li class="mui-table-view-cell mui-collapse" id="todoli_' + id + '" data-id="' + id + '">'
			+ '<a class="mui-navigate-right" href="#">'
			+ name
			+ '<span class="mui-badge mui-badge-danger">' + subDate + 'day</span>'
			+ '<p>' + realDate + '</p>'
			+ '</a>'
			+ '<div class="mui-collapse-content">'
			+ '<p>' + type + ':'+ dateOf + '</p>'
			+ '<p>' + note + '</p>'
			+ '</div>'
			+ '</li>';
		return li;
	}
})(mui, document);